package cn.kgc.demo01.controller;

import cn.kgc.demo01.pojo.Person;
import cn.kgc.demo01.pojo.ResultEntity;
import cn.kgc.demo01.service.PersonService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin("*")
@RestController
public class PersonController {
    @Resource
    PersonService personService;
    @RequestMapping("/selAll")
    @ApiOperation("动态查询并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "name",value = "姓名"),
            @ApiImplicitParam(name = "gender",value = "性别"),
            @ApiImplicitParam(name="age",value = "年龄"),
            @ApiImplicitParam(name="rank",value = "等级"),
            @ApiImplicitParam(name = "department",value = "所在部门"),
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false),
            @ApiImplicitParam(name = "pageSize",value = "页面行数",required = false)
    })
    public ResultEntity<PageInfo<Person>> selectAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "3")Integer pageSize,
                                                    @RequestParam(value = "department",required = false,defaultValue = "0")String department){
        PageInfo<Person> pageInfo=personService.select(pageNum,pageSize,department);
        return ResultEntity.successWithoutData(pageInfo);
    }
@RequestMapping("/save")
@ApiOperation("修改或增加")
@ApiImplicitParam(name = "id",value = "id")
    public ResultEntity<String> save(@RequestBody Person person){
try {
Integer integer=0;
if (person.getId()==null){
    integer=personService.add(person);
}else {
    integer=personService.update(person);
}
return  ResultEntity.successWithoutData(integer+"");
}catch (Exception ex){
    return  ResultEntity.failed(ex.getMessage());

}
}
@RequestMapping("/getById")
@ApiOperation("根据id查询")
@ApiImplicitParam(name = "id",value = "id",required = true)
    public ResultEntity<Person> getById(Integer id){
        Person person=personService.selectById(id);
        return ResultEntity.successWithoutData(person);
}
@RequestMapping("/delById")
@ApiOperation("删除")
@ApiImplicitParam(name = "id",value = "id")
    public ResultEntity <String> del(Integer id){
        try {
          Integer integer=personService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
}
}
